package com.nativetalk.controller.app.comment;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.comment.TdNtCommentPage;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.common.Constants;
import com.nativetalk.common.Lang;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.comment.insert.CommentInsertService;
import com.nativetalk.service.member.search.MemberSearchService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class CommentController {
    private static Logger logger = Logger.getLogger(CommentController.class);
    @Resource
    private CommentInsertService commentInsertService;

    @Resource
    private MemberSearchService memberSearchService;

    @RequestMapping(value = "/app/2/token/member/oneToOne/comment", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo commentOneTwoOne(@Valid @RequestBody TdNtCommentPage tdNtCommentPage, BindingResult result,
                                    @RequestHeader(value = "token", required = true) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtCommentPage));
            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;
            tdNtCommentPage.setMemb_id(tdNtMember.getMemb_id());
            //入参校验
            if (tdNtCommentPage.getReserve_id() == null || tdNtCommentPage.getReserve_id() == 0) {
                RetInfo retInfo = new RetInfo();
                retInfo.setTip("-1");
                retInfo.setMark("reserve_id can't be null");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            if (tdNtCommentPage.getTea_id() == null || tdNtCommentPage.getTea_id() == 0) {
                RetInfo retInfo = new RetInfo();
                retInfo.setTip("-1");
                retInfo.setMark("tea_id can't be null");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            if (tdNtCommentPage.getCou_id() == null || tdNtCommentPage.getCou_id() == 0) {
                RetInfo retInfo = new RetInfo();
                retInfo.setTip("-1");
                retInfo.setMark("cou_id can't be null");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            if (tdNtCommentPage.getCom_grade() == null || tdNtCommentPage.getCom_grade() == 0) {
                RetInfo retInfo = new RetInfo();
                retInfo.setTip("-1");
                retInfo.setMark("com_grade can't be null");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            if (StringUtils.isBlank(tdNtCommentPage.getCom_content())) {
                RetInfo retInfo = new RetInfo();
                retInfo.setTip("-1");
                retInfo.setMark("com_content can't be null");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (result.hasErrors()) {
                RetInfo retInfo = new RetInfo();
                List<FieldError> err = result.getFieldErrors();
                FieldError fe = err.get(0);
                retInfo.setMark(ValidationMessages.getValue("MARKPARAMETERERROR"));
                retInfo.setTip(fe.getDefaultMessage());
                //打印出参
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            //业务逻辑处理
            RetInfo retInfo = commentInsertService.insertComment(tdNtCommentPage);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    // 跳转到评测报告页面
    @RequestMapping(value = "/JspPage/{membId}/member/return/reportPage", method = {RequestMethod.GET, RequestMethod.POST})
    public String returnPortPage(@PathVariable("membId") Integer membId, Model model) {
        logger.info("~~~~~~~~~~~~~~~~~~~~~membId"+membId);
        TdNtMember tdNtMember = memberSearchService.findById(membId);
        model.addAttribute("token",tdNtMember.getToken());
        return "/app/returnReport/report";
    }

    /**
     * 获取评测报告数据
     * @param token
     * @return
     */
    @RequestMapping(value = "/app/2/token/member/return/port", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo returnPort(@RequestHeader(value = "token", required = true) String token) {
        try {
            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;
            Integer memb_id=tdNtMember.getMemb_id();
            RetInfo retInfo;
            retInfo = commentInsertService.returnCommentReport(memb_id);
            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }

}


